<?php
/**
 * Created by PhpStorm.
 * User: sunnicholas
 * Date: 2017/9/4
 * Time: 11:18
 */

class ControllerWorkSheetWorkSheet extends Controller   {
    private $error = array();

    public function index(){
        $this->load->language('work_sheet/work_sheet');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('work_sheet/work_sheet');

        $this->getList();
    }

    public function add(){
        $this->load->language('work_sheet/work_sheet');
        $this->document->setTitle($this->language->get('heading_title'));
        $this->load->model('work_sheet/work_sheet');
        $this->load->model('car/car');
        $this->load->model('site/parking_site');
        //&& $this->validateForm()
        if(($this->request->server['REQUEST_METHOD'] == 'POST')){

            $data = $this->request->post;


            $data['car_id'] = $this->model_car_car->getCarByIdNo($data['province_code_id'],$data['plate_no'])['car_id'];
            $data['parking_site_id'] = $this->model_site_parking_site->getParkingSiteBySiteName($data['parking_site_name'])['parking_site_id'];
            $data['work_sheet_status_id'] = 21;
//            echo '<pre/>';
//            var_dump($data);exit;
            $this->model_work_sheet_work_sheet->addWorkSheet($data);
            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->response->redirect($this->url->link('work_sheet/work_sheet', 'token=' . $this->session->data['token'] . $url, true));
        }
        $this->getForm();
    }

    public function delete() {

        $this->load->language('work_sheet/work_sheet');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('work_sheet/work_sheet');

        //&& $this->validateDelete()
        if (isset($this->request->post['selected']) ) {
            foreach ($this->request->post['selected'] as $work_sheet_id) {
                $this->model_work_sheet_work_sheet->deleteWorkSheet($work_sheet_id);
            }

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->response->redirect($this->url->link('work_sheet/work_sheet', 'token=' . $this->session->data['token'] . $url, true));
        }

        $this->getList();
    }

    public function edit() {

        $this->load->language('work_sheet/work_sheet');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('work_sheet/work_sheet');

// && $this->validateForm()
        if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {

            $this->model_work_sheet_work_sheet->editWorkSheet($this->request->get['work_sheet_id'], $this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['filter_work_sheet_no'])) {
                $url .= '&filter_work_sheet_no=' . urlencode(html_entity_decode($this->request->get['filter_work_sheet_no'], ENT_QUOTES, 'UTF-8'));
            }

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->response->redirect($this->url->link('work_sheet/work_sheet', 'token=' . $this->session->data['token'] . $url, true));
        }

        $this->getForm();
    }

    protected function getList(){

        $url = '';

        if (isset($this->request->get['filter_work_sheet_no']) && !empty($this->request->get['filter_work_sheet_no'])) {
            $filter_work_sheet_no = $this->request->get['filter_work_sheet_no'];
            $url .= `&filter_work_sheet_no =` . urlencode(html_entity_decode($this->request->get['filter_work_sheet_no']));
        } else {
            $filter_work_sheet_no = null;
        }


        if (isset($this->request->get['sort']) && !empty($this->request->get['sort'])) {
            $sort = $this->request->get['sort'];
            $url .= '&sort=' . $this->request->get['sort'];
        } else {
            $sort = 'work_sheet_id';
        }

        if (isset($this->request->get['order'])) {
            $order = $this->request->get['order'];
            $url .= '&order=' . $this->request->get['order'];
        } else {
            $order = 'ASC';
        }

        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
            $url .= '&page=' . $this->request->get['page'];
        } else {
            $page = 1;
        }

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
        );

        $data['breadcrumbs'][] = array(
            //跳转权限
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('work_sheet/work_sheet', 'token=' . $this->session->data['token'] . $url, true)
        );

        $data['add'] = $this->url->link('work_sheet/work_sheet/add', 'token=' . $this->session->data['token'] . $url, true);
        $data['delete'] = $this->url->link('work_sheet/work_sheet/delete', 'token=' . $this->session->data['token'] . $url, true);

        $data['work_sheets'] = array();

        $filter_data = array(
            'filter_work_sheet_no'          => $filter_work_sheet_no,
            'sort'  						=> $sort,
            'order' 						=> $order,
            'start' 						=> ($page - 1) * $this->config->get('config_limit_admin'),
            'limit' 						=> $this->config->get('config_limit_admin')
        );

        $guide_category_total = $this->model_work_sheet_work_sheet->getTotalWorkSheets();
        $results = $this->model_work_sheet_work_sheet->getWorkSheets($filter_data);

//        echo '<pre>';
//        var_dump($results);exit;
        foreach ($results as $result){
            $data['work_sheets'][] = array(
                'work_sheet_id'             =>      $result['work_sheet_id'],
                'work_sheet_no'             =>      $result['work_sheet_no'],
                'car_no'                    =>      $result['car_no'],
                'parking_site_no'           =>      $result['parking_site_no'],
                'parking_site_name'           =>    $result['parking_site_name'],
                'work_sheet_description'    =>      $result['work_sheet_description'],
                'work_sheet_category_name'    =>      $result['work_sheet_category_name'],
                'work_sheet_category_description'    =>      $result['work_sheet_category_description'],
                'work_sheet_status_id'          =>  $result['work_sheet_status_id'],
                'work_status_name'      =>      $result['work_status_name'],
                'work_status_description'      =>      $result['work_status_description'],
                'is_display'      =>      $result['is_display'],
                'work_status_display_name'      =>      $result['work_status_display_name'],
                'work_user_name'              =>      $result['work_user_name'],
                'comment'                   =>      $result['comment'],
                'create_user_name'            =>      $result['create_user_name'],
                'update_user_name'            =>      $result['update_user_name'],
                'status'     				=> ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
                'edit'       		=> $this->url->link('work_sheet/work_sheet/edit', 'token=' . $this->session->data['token'] . '&work_sheet_id=' . $result['work_sheet_id'] . $url, true),
                'delete'      		=> $this->url->link('work_sheet/work_sheet/delete', 'token=' . $this->session->data['token'] . '&work_sheet_id=' . $result['work_sheet_id'] . $url, true)
            );
        }

        $data['heading_title'] = $this->language->get('heading_title');

        $data['text_list'] = $this->language->get('text_list');
        $data['text_no_results'] = $this->language->get('text_no_results');
        $data['text_confirm'] = $this->language->get('text_confirm');
        $data['text_show'] = $this->language->get('text_show');
        $data['text_not_show'] = $this->language->get('text_not_show');

        $data['column_work_sheet_id'] = $this->language->get('column_work_sheet_id');
        $data['column_work_sheet_no'] = $this->language->get('column_work_sheet_no');
        $data['column_parking_site_no'] = $this->language->get('column_parking_site_no');
        $data['column_parking_site_name'] = $this->language->get('column_parking_site_name');
        $data['column_car_no'] = $this->language->get('column_car_no');
        $data['column_work_sheet_description'] = $this->language->get('column_work_sheet_description');
        $data['column_work_sheet_category_name'] = $this->language->get('column_work_sheet_category_name');
        $data['column_work_sheet_category_description'] = $this->language->get('column_work_sheet_category_description');
        $data['column_work_status_name'] = $this->language->get('column_work_status_name');
        $data['column_work_status_description'] = $this->language->get('column_work_status_description');
        $data['column_is_display'] = $this->language->get('column_is_display');
        $data['column_work_status_display_name'] = $this->language->get('column_work_status_display_name');
        $data['column_work_user_name'] = $this->language->get('column_work_user_name');
        $data['column_comment'] = $this->language->get('column_comment');
        $data['column_create_user_name'] = $this->language->get('column_create_user_name');
        $data['column_update_user_name'] = $this->language->get('column_update_user_name');
        $data['column_status'] = $this->language->get('column_status');
        $data['column_action'] = $this->language->get('column_action');

        $data['entry_work_sheet_no'] = $this->language->get('entry_work_sheet_no');
        $data['entry_car_no'] = $this->language->get('entry_car_no');
        $data['entry_is_show'] = $this->language->get('entry_is_show');

        $data['button_add'] = $this->language->get('button_add');
        $data['button_edit'] = $this->language->get('button_edit');
        $data['button_delete'] = $this->language->get('button_delete');
        $data['button_verify'] = $this->language->get('button_verify');
        $data['button_assign'] = $this->language->get('button_assign');

        $data['token'] = $this->session->data['token'];

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $data['success'] = '';
        }

        if (isset($this->request->post['selected'])) {
            $data['selected'] = (array)$this->request->post['selected'];
        } else {
            $data['selected'] = array();
        }

        $url = '';

        if ($order == 'ASC') {
            $url .= '&order=DESC';
        } else {
            $url .= '&order=ASC';
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $data['sort_work_sheet_id'] = $this->url->link('work_sheet/work_sheet', 'token=' . $this->session->data['token'] . '&sort=work_sheet_id' . $url, true);

        $url = '';

        //search
        if (isset($this->request->get['filter_work_sheet_no'])) {
            $url .= '&filter_work_sheet_no=' . urlencode(html_entity_decode($this->request->get['filter_work_sheet_no'], ENT_QUOTES, 'UTF-8'));
        }


        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        //new page
        $pagination = new Pagination();
        $pagination->total = $guide_category_total;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_limit_admin');
        $pagination->url = $this->url->link('work_sheet/work_sheet', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);

        $data['pagination'] = $pagination->render();

        $data['results'] = sprintf($this->language->get('text_pagination'), ($guide_category_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($guide_category_total - $this->config->get('config_limit_admin'))) ? $guide_category_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $guide_category_total, ceil($guide_category_total / $this->config->get('config_limit_admin')));

        $data['filter_member_name'] = $filter_work_sheet_no;

        $data['token'] = $this->session->data['token'];
        $data['change_status_url'] = $this->url->link('work_sheet/work_sheet/changeWorkStatus', 'token=' . $data['token'], true);
        $data['sort'] = $sort;
        $data['order'] = $order;

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('work_sheet/work_sheet_list.tpl', $data));
    }

    protected function getForm() {
        $data['heading_title'] = $this->language->get('heading_title');

        $data['text_form'] = !isset($this->request->get['work_sheet_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
        $data['text_none'] = $this->language->get('text_none');
        $data['text_enabled'] = $this->language->get('text_enabled');
        $data['text_disabled'] = $this->language->get('text_disabled');
        $data['text_select'] = $this->language->get('text_select');

        $data['entry_work_sheet_no'] = $this->language->get('entry_work_sheet_no');
        $data['entry_car_no'] = $this->language->get('entry_car_no');
        $data['entry_plate_no'] = $this->language->get('entry_plate_no');
        $data['entry_parking_site_no'] = $this->language->get('entry_parking_site_no');
        $data['entry_parking_site_name'] = $this->language->get('entry_parking_site_name');
        $data['entry_work_sheet_description'] = $this->language->get('entry_work_sheet_description');
        $data['entry_work_sheet_category_name'] = $this->language->get('entry_work_sheet_category_name');
        $data['entry_work_user_name'] = $this->language->get('entry_work_user_name');
        $data['entry_status'] = $this->language->get('entry_status');


        $data['button_save'] = $this->language->get('button_save');
        $data['button_cancel'] = $this->language->get('button_cancel');

        $data['tab_general'] = $this->language->get('tab_general');
        $data['tab_data'] = $this->language->get('tab_data');

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->error['work_sheet_no'])) {
            $data['error_work_sheet_no'] = $this->error['work_sheet_no'];
        } else {
            $data['error_work_sheet_no'] = array();
        }

        if (isset($this->error['plate_no'])) {
            $data['error_plate_no'] = $this->error['plate_no'];
        } else {
            $data['error_plate_no'] = '';
        }

        $url = '';

        if (isset($this->request->get['filter_work_sheet_no'])) {
            $url .= '&filter_work_sheet_no=' . urlencode(html_entity_decode($this->request->get['filter_work_sheet_no'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('work_sheet/work_sheet', 'token=' . $this->session->data['token'] . $url, true)
        );

        if (!isset($this->request->get['work_sheet_id'])) {
            $data['action'] = $this->url->link('work_sheet/work_sheet/add', 'token=' . $this->session->data['token'] . $url, true);
        } else {
            $data['action'] = $this->url->link('work_sheet/work_sheet/edit', 'token=' . $this->session->data['token'] . '&work_sheet_id=' . $this->request->get['work_sheet_id'] . $url, true);
        }

        $data['cancel'] = $this->url->link('work_sheet/work_sheet', 'token=' . $this->session->data['token'] . $url, true);

        if (isset($this->request->get['work_sheet_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $work_sheet_info = $this->model_work_sheet_work_sheet->getWorkSheet($this->request->get['work_sheet_id']);
        }

        $data['token'] = $this->session->data['token'];

        $this->load->model('localisation/language');

        $data['languages'] = $this->model_localisation_language->getLanguages();

        if (isset($this->request->post['work_sheet_no'])) {
            $data['work_sheet_no'] = $this->request->post['work_sheet_no'];
        } elseif (!empty($work_sheet_info)) {
            $data['work_sheet_no'] = $work_sheet_info['work_sheet_no'];
        } else {
            $data['work_sheet_no'] = $this->model_work_sheet_work_sheet->generateWorkSheetNo();
        }

        $this->load->model('localisation/province_code');
        $data['province_code_list'] = $this->model_localisation_province_code->getProvinceCodes();

        if (isset($this->request->post['province_code_id'])) {
            $data['province_code_id'] = $this->request->post['province_code_id'];
        } elseif (!empty($work_sheet_info)) {
            $data['province_code_id'] = $work_sheet_info['province_code_id'];
        } else {
            $data['province_code_id'] = 0;
        }

        $this->load->model('work_sheet/work_sheet_category');
        $data['work_sheet_category_list'] = $this->model_work_sheet_work_sheet_category->getWorkSheetCategorys();
        if (isset($this->request->post['work_sheet_category_id'])) {
            $data['work_sheet_category_id'] = $this->request->post['work_sheet_category_id'];
        } elseif (!empty($work_sheet_info)) {
            $data['work_sheet_category_id'] = $work_sheet_info['work_sheet_category_id'];
        } else {
            $data['work_sheet_category_id'] = 0;
        }

//        if (isset($this->request->post['work_sheet_status_id'])) {
//            $data['work_sheet_status_id'] = $this->request->post['work_sheet_status_id'];
//        } elseif (!empty($work_sheet_info)) {
//            $data['work_sheet_status_id'] = $work_sheet_info['work_sheet_status_id'];
//        } else {
//            $data['work_sheet_status_id'] = 21;
//        }

        $this->load->model('user/user');
        $data['work_user_name_list'] = $this->model_user_user->getUsers();

        if (isset($this->request->post['work_user_id'])) {
            $data['work_user_id'] = $this->request->post['work_user_id'];
        } elseif (!empty($work_sheet_info)) {
            $data['work_user_id'] = $work_sheet_info['work_user_id'];
        } else {
            $data['work_user_id'] = 0;
        }

        if (isset($this->request->post['plate_no'])) {
            $data['plate_no'] = $this->request->post['plate_no'];
        } elseif (!empty($work_sheet_info)) {
            $data['plate_no'] = $work_sheet_info['plate_no'];
        } else {
            $data['plate_no'] = "";
        }

        if (isset($this->request->post['parking_site_name'])) {
            $data['parking_site_name'] = $this->request->post['parking_site_name'];

        } elseif (!empty($work_sheet_info)) {
            $data['parking_site_name'] = $work_sheet_info['parking_site_name'];
        } else {
            $data['parking_site_name'] = '';
        }

//        $this->load->model('user/user');
//        $data['parking_site_id'] = $this->model_site_parking_site->getParkingSiteBySiteName();

        if (isset($this->request->post['work_sheet_description'])) {
            $data['work_sheet_description'] = $this->request->post['work_sheet_description'];
        } elseif (!empty($work_sheet_info)) {
            $data['work_sheet_description'] = $work_sheet_info['work_sheet_description'];
        } else {
            $data['work_sheet_description'] = '';
        }

        if (isset($this->request->post['work_sheet_category_name'])) {
            $data['work_sheet_category_name'] = $this->request->post['work_sheet_category_name'];
        } elseif (!empty($work_sheet_info)) {
            $data['work_sheet_category_name'] = $work_sheet_info['work_sheet_category_name'];
        } else {
            $data['work_sheet_category_name'] = '';
        }


        if (isset($this->request->post['work_user_name'])) {
            $data['work_user_name'] = $this->request->post['work_user_name'];
        } elseif (!empty($work_sheet_info)) {
            $data['work_user_name'] = $work_sheet_info['username'];
        } else {
            $data['work_user_name'] = '';
        }

        if (isset($this->request->post['status'])) {
            $data['status'] = $this->request->post['status'];
        } elseif (!empty($work_sheet_info)) {
            $data['status'] = $work_sheet_info['status'];
        } else {
            $data['status'] = true;
        }

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('work_sheet/work_sheet_form.tpl', $data));
    }

    public function changeWorkStatus() {

        $this->load->language('work_sheet/work_sheet');
        $this->document->setTitle($this->language->get('heading_title'));
        $this->load->model('work_sheet/work_sheet');
        $this->load->model('user/user');

        if($this->request->server['REQUEST_METHOD'] == 'POST') {
//            $this->session->data['success'] = $this->language->get('text_success');
            $work_sheet = array();
            var_dump($this->request->post);
            $work_sheet['work_sheet_id'] = $this->request->post['work_sheet_id'];
            $work_sheet['work_status'] = (int)$this->request->post['work_status'];
            $work_sheet['update_user_id'] = $this->user->getId();
            $work_sheet['work_sheet_status_id'] = ((int)$this->request->post['work_status']==11)? 21 : ((int)$this->request->post['work_status']==21?31:(int)$this->request->post['work_status']);
//            var_dump($work_sheet);exit;
            if($work_sheet['work_status'] == '11'){
                $this->model_work_sheet_work_sheet->assignWorkSheet($work_sheet['work_sheet_id'],$work_sheet);
//                //>>操作成功
                $this->session->data['success'] = $this->language->get('text_success');
                $data = array('status' => 1,'message' => $this->session->data['success']);
                echo json_encode($data);
            }elseif($work_sheet['work_status'] == '21'){
                $this->model_work_sheet_work_sheet->assignWorkSheet($work_sheet['work_sheet_id'],$work_sheet);
//                //>>操作成功
                $this->session->data['success'] = $this->language->get('text_success');
                $data = array('status' => 1,'message' => $this->session->data['success']);
                echo json_encode($data);
            }

        }

    }


}